//
//  HJGroupMemberSearchController.m
//  KaiChat
//
//  Created by lx on 2023/11/7.
//

#import "HJGroupMemberSearchController.h"
#import "HJGroupMemberCell.h"
#import "QMUITextField.h"
#import "TJEmptyDataHandler.h"
#import "HJNavigationController.h"
#import "HJSearchView.h"
#import "HJUserInfoController.h"

@interface HJGroupMemberSearchController ()<QMUITextFieldDelegate>

@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;
@property (nonatomic, weak) QMUITextField *textField;
@property (nonatomic, strong) NSMutableArray *searchDatas;

@end

@implementation HJGroupMemberSearchController

- (void)showInController:(UIViewController *)controller {
    HJNavigationController *navController = [[HJNavigationController alloc] initWithRootViewController:self];
    [controller presentViewController:navController animated:YES completion:nil];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.lineView.hidden = YES;
    
    HJSearchView *searchView = [HJSearchView addToView:self.navigationBar.contentView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.left.offset(48);
        make.centerY.offset(0);
        make.height.offset(40);
    }];

    QMUITextField *textField = searchView.textField;
    textField.placeholder = @"Search";
    textField.maximumTextLength = 30;
    textField.returnKeyType = UIReturnKeyDone;
    textField.delegate = self;
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    _textField = textField;
    
    _emptyDataHandler =  [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.iconName = @"search_empty";
        dataModel.text = @"Results not found";
        return TJEmptyDataTypeBase;
    }];
 
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.tableView registerClass:HJGroupMemberCell.class forCellReuseIdentifier:@"HJGroupMemberCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    dispatch_async_main_queue(^{
        [self.textField becomeFirstResponder];
    });
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidChange:(QMUITextField *)textField {
    
    NSString *keyword = textField.text;

    if (isEmptyString(keyword)) {
        for (HJGroupMemberModel *obj in self.datas) {
            obj.nicknameAttributedstring = nil;
        }
        self.emptyDataHandler.canDisplay = NO;
        [self.searchDatas removeAllObjects];
        [self.tableView reloadData];
        return;
    }
    
    [self.searchDatas removeAllObjects];
    for (HJGroupMemberModel *obj in self.datas) {
        NSRange range = [obj.nickname rangeOfString:keyword options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:obj.nickname];
            [attrStr addAttribute:NSForegroundColorAttributeName value:UIColor.tj_mainColor range:range];
            obj.nicknameAttributedstring = attrStr;
            [self.searchDatas addObject:obj];
        } else {
            obj.nicknameAttributedstring = nil;
        }
    }
    
    self.emptyDataHandler.canDisplay = YES;
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupMemberCell" forIndexPath:indexPath];
    HJGroupMemberModel *model = self.searchDatas[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJGroupMemberModel *model = self.searchDatas[indexPath.row];
    HJUserInfoController *infoController = [[HJUserInfoController alloc] init];
    infoController.userId = model.userId;
    [self.navigationController pushViewController:infoController];
}


- (UIModalPresentationStyle)modalPresentationStyle {
   return UIModalPresentationFullScreen;
}

- (NSMutableArray *)searchDatas {
    if (!_searchDatas) {
        _searchDatas = [NSMutableArray array];
    }
    return _searchDatas;
}

@end
